﻿using UnityEngine;

public class GMToolsCtrl : CSIMediator
{
    private GMToolsPanel view;
    private UIInput input;
    private TimerVO timerVO;
    
    public override void Awake()
    {
        base.Awake(); view = new GMToolsPanel(Go);
        input = view.Input_txt.GetComponent<UIInput>();
    }

    public override void Open()
    {
        base.Open(); 
        input.value = "";
    }

    public override void Close()
    {
        base.Close();
    }

    public override void Destroy()
    {
        view = null;
        input = null;
        timerVO = null;
        base.Destroy();
    }

    public override void AddListener()
    {
        timerVO = new TimerVO(1, TimerUpdate, true);
        EventDelegate.Add(input.onSubmit, OnSubmit);
        UIEventListener.Get(view.OK_obj).onClick = OnClick;
        UIEventListener.Get(view.Close_obj).onClick = OnClick;
    }

    public override int PanelId
    {
        get { return (int)PanelID.CmdPanel; }
    }
    
    private void OnClick(GameObject go)
    {
        if (go == view.Close_obj)
        {
            Close(); //关闭gm界面
        }
        else if (go == view.OK_obj)
        {
            OnSubmit();//发送gm指令
        }
    }

    private void TimerUpdate()
    {
        view.Time_txt.text = ZMgr.TimeMgr.GetServerTimeToString();
    }
    
    private void OnSubmit()
    {
        string cmdStr = view.Input_txt.text;
        if (!string.IsNullOrEmpty(cmdStr))
        {
            view.Output_txt.text = cmdStr;
        }
        input.value = "";
    }
}